<?php
namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

use App\Models;


class ArtController extends Controller
{
    public function index(Request $request,$keyboard=null){
		
		$art = Models\Art::select('art_id','title','content','author','cat_id','comm','view','created_at','updated_at')
			->orderByDesc('view')->where([['is_state', '=', '0'],['is_del', '=', '1']])->find($keyboard);
		if(!$art) { abort(404);}	//文章不存在
		$fronts['art'] = $art;
		
		//浏览量 5分钟自增加2
		$minute = intval((strtotime(date('Y-m-d H:i:s')) - strtotime($art['updated_at']))/60);//分钟
        if ($minute >= 5) {//5分钟间隔
			$art->increment('view', 2);
        }
		
		$tdk['title'] = $art['title'].'_'.$this->cats[$art['cat_id']]['catname'].' - 代码示例网';
		$tdk['keywords'] = $this->cats[$art['cat_id']]['catname'];
		$tdk['description'] = $this->cats[$art['cat_id']]['catname'].'_'.delcontent_html($art['content'],140);
		$fronts['tdk'] = $tdk;
		
		//上一条下一条
		$fronts['up'] = $art->where('art_id', '<', $art['art_id'])->orderByDesc('art_id')->first();
		$fronts['down'] = $art->where('art_id', '>', $art['art_id'])->orderByDesc('art_id')->first();
		
		//相关文章
        $fronts['relatedarts'] = Models\Art::select('art_id','title','cat_id')->where('cat_id', $art['cat_id'])
				->orderByDesc('view')->where([['is_state', '=', '0'],['is_del', '=', '1']])->limit(6)->get();
	    
		//评论
		$comments = $art->comments->toArray();
		$fronts['comments'] = $this->sontree_comment($comments,'comment_id','fcomment_id');
		
		
		//热点缓存
		if (!Cache()->has('hotarts')) {
			$fronts['hotarts'] = Models\Art::select('art_id','title')->orderByDesc('view')->where([['is_state', '=', '0'],['is_del', '=', '1']])->limit(5)->get()->toArray();
			Cache()->set('hotarts',$fronts['hotarts'],3600);
		}
		$fronts['hotarts'] = Cache()->get('hotarts');
		
		
        return view('front/art/index',$fronts);
    }
	
	//写评论
    public function comment(Request $request)
    {
		//"_token" => "nhdahS1YJbKA9yLsuk5bsxx6ayLFntoYDUOTa0yH""art_id" => "96136""comment" => "<p>33在</p>"
		$query = $request->input();
		$query['fcomment_id'] = $query['fcomment_id']??0;

		//验证
		$validator = Validator::make($query, 
		 [//rules
			 'art_id' => 'required',
			 'comment' => 'required|max:1000',
		 ], 
		 [//messages
			'art_id.required' => '非法错误',
			'comment.required' => '评论不能为空',
			'comment.max' => '评论长度限制1000个字符',
		 ]);
		 if ($validator->fails()) {
			return array('error' => 0, 'info' => $validator->errors()->first());
		 }
		
		//入库
		$comment = Models\Comment::create([
			'fcomment_id' => $query['fcomment_id'],
			'art_id' => $query['art_id'],
			'comment' => $query['comment'],
			'user_id' => Auth::user()->id,
			'username' => Auth::user()->name,
			'email' => Auth::user()->email,
			'created_at' => date('Y-m-d H:i:s'),
			'ip' => $request->getClientIp(),
			'status' => 1,
		]);
		
		if ($comment) {
			//评论成功
		}else{
			//失败
		}
		
		//重定向到上一页，如果没有就当前页
		return redirect()->back();
	}
	

}
